home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / etc / init.d / alsa-utils < prev    next >
Encoding:
Text File  |  2009-04-07  |  9.9 KB  |  398 lines

  1. #!/bin/sh
  2. #
  3. # alsa-utils initscript
  4. #
  5. ### BEGIN INIT INFO
  6. # Provides:          alsa-utils
  7. # Required-Start:    $remote_fs
  8. # Required-Stop:     $remote_fs
  9. # Default-Start:     
  10. # Default-Stop:      0 6
  11. # Short-Description: Restore and store ALSA driver settings
  12. # Description:       This script stores and restores mixer levels on
  13. #                    shutdown and bootup.On sysv-rc systems: to
  14. #                    disable storing of mixer levels on shutdown,
  15. #                    remove /etc/rc[06].d/K50alsa-utils.  To disable
  16. #                    restoring of mixer levels on bootup, rename the
  17. #                    "S50alsa-utils" symbolic link in /etc/rcS.d/ to
  18. #                    "K50alsa-utils".
  19. ### END INIT INFO
  20.  
  21. # Don't use set -e; check exit status instead
  22.  
  23. # Exit silently if package is no longer installed
  24. [ -x /sbin/alsactl ] || exit 0
  25.  
  26. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  27. MYNAME=/etc/init.d/alsa-utils
  28. export PULSE_INTERNAL=0
  29.  
  30. . /lib/lsb/init-functions
  31.  
  32. # $1 EXITSTATUS
  33. # [$2 MESSAGE]
  34. log_action_end_msg_and_exit()
  35. {
  36.     log_action_end_msg "$1" ${2:+"$2"}
  37.     exit $1
  38. }
  39.  
  40. # $1 PROGRAM
  41. executable()
  42. {
  43.     # If which is not available then we must be running before
  44.     # /usr is mounted on a system that has which in /usr/bin/.
  45.     # Conclude that $1 is not executable.
  46.     [ -x /bin/which ] || [ -x /usr/bin/which ] || return 1
  47.     which "$1" >/dev/null 2>&1
  48. }
  49.  
  50. # Wait for filesystems to show up
  51. while [ ! -d /usr/bin -o ! -d /var/lib/alsa ]; do
  52.     sleep 0.2
  53. done
  54.  
  55. executable amixer || { echo "${MYNAME}: Error: No amixer program available." >&2 ; exit 1 ; }
  56.  
  57. bugout() { echo "${MYNAME}: Programming error" >&2 ; exit 123 ; }
  58.  
  59. # $1 <card ID> | "all"
  60. restore_levels()
  61. {
  62.     [ -f /var/lib/alsa/asound.state ] || return 1
  63.     CARD="$1"
  64.     [ "$1" = all ] && CARD=""
  65.     # Assume that if alsactl prints a message on stderr
  66.     # then it failed somehow.  This works around the fact
  67.     # that alsactl doesn't return nonzero status when it
  68.     # can't restore settings for the card
  69.     if MSG="$(alsactl restore $CARD 2>&1 >/dev/null)" && [ ! "$MSG" ] ; then
  70.         return 0
  71.     else
  72.         # Retry with the "force" option.  This restores more levels
  73.         # but it results in much longer error messages.
  74.         alsactl -F restore $CARD >/dev/null 2>&1
  75.         [ -z "$CARD" ] && log_action_cont_msg "warning: 'alsactl restore' failed with error message '$MSG'"
  76.         return 1
  77.     fi
  78. }
  79.  
  80. # $1 <card ID> | "all"
  81. store_levels()
  82. {
  83.     CARD="$1"
  84.     [ "$1" = all ] && CARD=""
  85.     if MSG="$(alsactl store $CARD 2>&1)" ; then
  86.         sleep 1
  87.         return 0
  88.     else
  89.         [ -z "$CARD" ] && log_action_cont_msg "warning: 'alsactl store' failed with error message '$MSG'"
  90.         return 1
  91.     fi
  92. }
  93.  
  94. echo_card_indices()
  95. {
  96.     if [ -f /proc/asound/cards ] ; then
  97.         sed -n -e's/^[[:space:]]*\([0-7]\)[[:space:]].*/\1/p' /proc/asound/cards
  98.     fi
  99. }
  100.  
  101. filter_amixer_output()
  102. {
  103.     sed \
  104.         -e '/Unable to find simple control/d' \
  105.         -e '/Unknown playback setup/d' \
  106.         -e '/^$/d'
  107. }
  108.  
  109. # The following functions try to set many controls.
  110. # No card has all the controls and so some of the attempts are bound to fail.
  111. # Because of this, the functions can't return useful status values.
  112.  
  113. # $1 <control>
  114. # $2 <level>
  115. # $CARDOPT
  116. unmute_and_set_level()
  117. {
  118.     { [ "$2" ] && [ "$CARDOPT" ] ; } || bugout
  119.     amixer $CARDOPT -q set "$1" "$2" unmute 2>&1 | filter_amixer_output || :
  120.     return 0
  121. }
  122.  
  123. # $1 <control>
  124. # $CARDOPT
  125. mute_and_zero_level()
  126. {
  127.     { [ "$1" ] && [ "$CARDOPT" ] ; } || bugout
  128.     amixer $CARDOPT -q set "$1" "0%" mute 2>&1 | filter_amixer_output || :
  129.     return 0
  130. }
  131.  
  132. # $1 <control>
  133. # $2 "on" | "off"
  134. # $CARDOPT
  135. switch_control()
  136. {
  137.     { [ "$2" ] && [ "$CARDOPT" ] ; } || bugout
  138.     amixer $CARDOPT -q set "$1" "$2" 2>&1 | filter_amixer_output || :
  139.     return 0
  140. }
  141.  
  142. # $1 <card ID>
  143. sanify_levels_on_card()
  144. {
  145.     CARDOPT="-c $1"
  146.  
  147.     unmute_and_set_level "Front" "80%"
  148.     unmute_and_set_level "Master" "80%"
  149.     unmute_and_set_level "Master Mono" "80%"   # See Bug#406047
  150.     unmute_and_set_level "Master Digital" "80%"   # E.g., cs4237B
  151.     unmute_and_set_level "Playback" "80%"
  152.     unmute_and_set_level "Headphone" "70%"
  153.     unmute_and_set_level "PCM" "80%"
  154.     unmute_and_set_level "PCM,1" "80%"   # E.g., ess1969
  155.     unmute_and_set_level "DAC" "80%"     # E.g., envy24, cs46xx
  156.     unmute_and_set_level "DAC,0" "80%"   # E.g., envy24
  157.     unmute_and_set_level "DAC,1" "80%"   # E.g., envy24
  158.     unmute_and_set_level "Synth" "80%"
  159.     unmute_and_set_level "CD" "80%"
  160.  
  161.     mute_and_zero_level "Mic"
  162.     mute_and_zero_level "IEC958"         # Ubuntu #19648
  163.  
  164.     # Intel P4P800-MX  (Ubuntu bug #5813)
  165.     switch_control "Master Playback Switch" on
  166.     switch_control "Master Surround" on
  167.  
  168.     # Trident/YMFPCI/emu10k1:
  169.     unmute_and_set_level "Wave" "80%"
  170.     unmute_and_set_level "Music" "80%"
  171.     unmute_and_set_level "AC97" "80%"
  172.  
  173.     # DRC:
  174.     unmute_and_set_level "Dynamic Range Compression" "80%"
  175.  
  176.     # Required for HDA Intel (hda-intel):
  177.     unmute_and_set_level "Front" "80%"
  178.  
  179.     # Required for SB Live 7.1/24-bit (ca0106):
  180.     unmute_and_set_level "Analog Front" "80%"
  181.  
  182.     # Required at least for Via 823x hardware on DFI K8M800-MLVF Motherboard with kernels 2.6.10-3/4 (see ubuntu #7286):
  183.     switch_control "IEC958 Capture Monitor" off
  184.  
  185.     # Required for hardware allowing toggles for AC97 through IEC958,
  186.     #  valid values are 0, 1, 2, 3. Needs to be set to 0 for PCM1.
  187.     unmute_and_set_level "IEC958 Playback AC97-SPSA" "0"
  188.  
  189.     # Required for newer Via hardware (see Ubuntu #31784)
  190.     unmute_and_set_level "VIA DXS,0" "80%"
  191.     unmute_and_set_level "VIA DXS,1" "80%"
  192.     unmute_and_set_level "VIA DXS,2" "80%"
  193.     unmute_and_set_level "VIA DXS,3" "80%"
  194.  
  195.     # Required on some notebooks with ICH4:
  196.     switch_control "Headphone Jack Sense" off
  197.     switch_control "Line Jack Sense" off
  198.  
  199.     # Some machines need one or more of these to be on;
  200.     # others need one or more of these to be off:
  201.     #
  202.     # switch_control "External Amplifier" on
  203.     switch_control "Audigy Analog/Digital Output Jack" off
  204.     switch_control "SB Live Analog/Digital Output Jack" off
  205.     
  206.     # D1984 -- Thinkpad T61/X61
  207.     # also needed for Dell Mini 9 and Dell E series
  208.     unmute_and_set_level "Speaker" "80%"
  209.     unmute_and_set_level "Headphone" "80%"
  210.  
  211.     # HDA-Intel w/ "Digital" capture mixer (See Ubuntu #193823)
  212.     unmute_and_set_level "Digital" "80%"
  213.  
  214.     # HDA-Intel w/ Digital Mic should default to Digital mic rather than
  215.     # analog (See Ubuntu #314188)
  216.     switch_control "Digital Input Source" "Digital Mic 1"
  217.     switch_control "Digital Input Source" "Digital Mic"
  218.  
  219.     # Turn off PC Beep on hda cards that support it, see Ubuntu #331589.
  220.     mute_and_zero_level "PC Beep"
  221.  
  222.     return 0
  223. }
  224.  
  225. # $1 <card ID> | "all"
  226. sanify_levels()
  227. {
  228.     TTSDML_RETURNSTATUS=0
  229.     case "$1" in
  230.       all)
  231.         for CARD in $(echo_card_indices) ; do
  232.             sanify_levels_on_card "$CARD" || TTSDML_RETURNSTATUS=1
  233.         done
  234.  
  235.         ;;
  236.       *)
  237.         sanify_levels_on_card "$1" || TTSDML_RETURNSTATUS=1
  238.         ;;
  239.     esac
  240.     mute_and_zero_levels_on_card "pcsp"
  241.     return $TTSDML_RETURNSTATUS
  242. }
  243.  
  244. # $1 <card ID>
  245. preinit_levels_on_card()
  246. {
  247.     CARDOPT="-c $1"
  248.  
  249.     # Silly dance to activate internal speakers by default on PowerMac
  250.     # Snapper and Tumbler
  251.     id=`cat /proc/asound/card$1/id 2>/dev/null`
  252.     if [ "$id" = "Snapper" -o "$id" = "Tumbler" ]; then
  253.         switch_control "Auto Mute" off
  254.         switch_control "PC Speaker" off
  255.         switch_control "Auto Mute" on
  256.     fi
  257. }
  258.  
  259. # $1 <card ID> | "all"
  260. preinit_levels()
  261. {
  262.     TTSDML_RETURNSTATUS=0
  263.     case "$1" in
  264.       all)
  265.         for CARD in $(echo_card_indices) ; do
  266.             preinit_levels_on_card "$CARD" || TTSDML_RETURNSTATUS=1
  267.         done
  268.         ;;
  269.       *)
  270.         preinit_levels_on_card "$1" || TTSDML_RETURNSTATUS=1
  271.         ;;
  272.     esac
  273.     return $TTSDML_RETURNSTATUS
  274. }
  275.  
  276. # $1 <card ID>
  277. mute_and_zero_levels_on_card()
  278. {
  279.     CARDOPT="-c $1"
  280.     for CTL in \
  281.         Master \
  282.         PCM \
  283.         Synth \
  284.         CD \
  285.         Line \
  286.         Mic \
  287.         "PCM,1" \
  288.         Wave \
  289.         Music \
  290.         AC97 \
  291.         "Master Digital" \
  292.         DAC \
  293.         "DAC,0" \
  294.         "DAC,1" \
  295.         Headphone \
  296.         Speaker \
  297.         Playback
  298.     do
  299.         mute_and_zero_level "$CTL"
  300.     done
  301. #    for CTL in \
  302. #        "Audigy Analog/Digital Output Jack" \
  303. #        "SB Live Analog/Digital Output Jack"
  304. #    do
  305. #        switch_control "$CTL" off
  306. #    done
  307.     return 0
  308. }
  309.  
  310. # $1 <card ID> | "all"
  311. mute_and_zero_levels()
  312. {
  313.     TTZML_RETURNSTATUS=0
  314.     case "$1" in
  315.       all)
  316.         for CARD in $(echo_card_indices) ; do
  317.             mute_and_zero_levels_on_card "$CARD" || TTZML_RETURNSTATUS=1
  318.         done
  319.         ;;
  320.       *)
  321.         mute_and_zero_levels_on_card "$1" || TTZML_RETURNSTATUS=1
  322.         ;;
  323.     esac
  324.     return $TTZML_RETURNSTATUS
  325. }
  326.  
  327.  
  328. # $1 <card ID> | "all"
  329. card_OK()
  330. {
  331.     [ "$1" ] || bugout
  332.     if [ "$1" = all ] ; then
  333.         [ -d /proc/asound ]
  334.         return $?
  335.     else
  336.         [ -d "/proc/asound/card$1" ] || [ -d "/proc/asound/$1" ]
  337.         return $?
  338.     fi
  339. }
  340.  
  341. # If a card identifier is provided in $2 then regard it as an error
  342. # if that card is not present; otherwise don't regard it as an error.
  343.  
  344. case "$1" in
  345.   start)
  346.     EXITSTATUS=0
  347.     TARGET_CARD="$2"
  348.     case "$TARGET_CARD" in
  349.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Setting up ALSA" ;;
  350.     esac
  351.     if ! card_OK "$TARGET_CARD"; then
  352.         [ "$TARGET_CARD" = "all" ] && log_action_end_msg "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  353.         exit $?
  354.     fi
  355.     preinit_levels "$TARGET_CARD" || EXITSTATUS=1
  356.     if ! restore_levels "$TARGET_CARD" ; then
  357.         sanify_levels "$TARGET_CARD" || EXITSTATUS=1
  358.         restore_levels "$TARGET_CARD" >/dev/null 2>&1 || :
  359.     fi
  360.     [ "$TARGET_CARD" = "all" ] && log_action_end_msg_and_exit "$EXITSTATUS"
  361.     exit $EXITSTATUS
  362.     ;;
  363.   stop)
  364.     EXITSTATUS=0
  365.     TARGET_CARD="$2"
  366.     case "$TARGET_CARD" in
  367.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Shutting down ALSA" ;;
  368.       *) log_action_begin_msg "Shutting down ALSA card ${TARGET_CARD}" ;;
  369.     esac
  370.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  371.     store_levels "$TARGET_CARD" || EXITSTATUS=1
  372.     mute_and_zero_levels "$TARGET_CARD" || EXITSTATUS=1
  373.     log_action_end_msg_and_exit "$EXITSTATUS"
  374.     ;;
  375.   restart|force-reload)
  376.     EXITSTATUS=0
  377.     $0 stop || EXITSTATUS=1
  378.     $0 start || EXITSTATUS=1
  379.     exit $EXITSTATUS
  380.     ;;
  381.   reset)
  382.     TARGET_CARD="$2"
  383.     case "$TARGET_CARD" in
  384.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Resetting ALSA" ;;
  385.       *) log_action_begin_msg "Resetting ALSA card ${TARGET_CARD}" ;;
  386.     esac
  387.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  388.     preinit_levels "$TARGET_CARD"
  389.     sanify_levels "$TARGET_CARD"
  390.     log_action_end_msg_and_exit "$?"
  391.     ;;
  392.   *)
  393.     echo "Usage: $MYNAME {start [CARD]|stop [CARD]|restart [CARD]|reset [CARD]}" >&2
  394.     exit 3
  395.     ;;
  396. esac
  397.  
  398.